<?php
/**
 * Created by PhpStorm.
 * User: xt
 * Date: 2017/5/2
 * Time: 9:10
 */
use core\Cache;
use core\Session;
use core\Cookie;
use core\Config;
use core\View;
use core\http\Response;
use core\http\Request;

if (!function_exists('createGuid')) {
    /**
     * @return string
     */
    function createGuid()
    {
        if ( function_exists('com_create_guid') ){
            return com_create_guid();
        } else {
            $charId = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);// "-"
            $uuid = chr(123)// "{"
                .substr($charId, 0, 8) . $hyphen
                .substr($charId, 8, 4) . $hyphen
                .substr($charId,12, 4) . $hyphen
                .substr($charId,16, 4) . $hyphen
                .substr($charId,20,12)
                .chr(125);// "}"
            return str_replace(array('{', '}', '-'), '',$uuid);
        }
    }
}
if (! function_exists('cache')) {

    /**
     * @param $key
     * @param string $value
     * @param int $expire
     * @return mixed
     */
    function cache($key, $value = '', $expire = 3600)
    {
        $cache = Cache::getInstance();
        if (is_null($value)) {
            return $cache->del($key);
        } else if (!empty($value)) {
            return $cache->set($key, $value, null, $expire);
        } else {
            return $cache->get($key);
        }
    }

}


if (! function_exists('cookie')) {

    /**
     * @param $key
     * @param string $value
     * @param int $expire
     * @return array|mixed|null
     */
    function cookie($key, $value = '', $expire = 3600)
    {
        if (is_null($value)) {
            return Cookie::delete($key);
        } else if (!empty($value)) {
            return Cookie::set($key, $value, $expire);
        } else {
            return Cookie::get($key);
        }
    }

}

if (! function_exists('session')) {

    /**
     * @param $key
     * @param string $value
     * @return bool|mixed|null
     */
    function session($key, $value = '')
    {
        if (is_null($value)) {
            return Session::getInstance()->destroy();
        } else if (!empty($value)) {
            return Session::getInstance()->set($key, $value);
        } else {
            return Session::getInstance()->get($key);
        }
    }

}

if (! function_exists('config')) {

    /**
     * @param $name
     * @return bool|mixed
     */
    function config($name)
    {
        return Config::get($name);
    }

}

if (! function_exists('import')) {

    /**
     * @param $path
     * @param string $basePath
     * @return bool
     */
    function import($path, $basePath = ROOT_PATH . 'other/')
    {
        static $includes = [];
        $path =  $basePath . ltrim($path, '/');
        $key = md5($path);
        if (isset($includes[$key])) return true;

        if (file_exists($path)) {
            include $path . '';
            $includes[$key] = true;
            return true;
        } else {
            return false;
        }
    }

}

if (! function_exists('view')) {

    /**
     * @param $template
     * @param array $arguments
     * @return string
     */
    function view($template, $arguments = [])
    {
        return View::getInstance()->fetch($template, $arguments);
    }

}


if (! function_exists('request')) {

    /**
     * @return static
     */
    function request()
    {
        return Request::getInstance();
    }

}

if (! function_exists('response')) {

    /**
     * @return static
     */
    function response()
    {
        return Response::getInstance();
    }

}
